home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk10.zip / INIT.C < prev    next >
C/C++ Source or Header  |  1991-10-05  |  6KB  |  267 lines

  1.  
  2. /********************************************
  3. init.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13.  
  14. /* $Log:    init.c,v $
  15.  * Revision 3.6.1.1  91/09/14  17:23:28  brennan
  16.  * VERSION 1.0
  17.  * 
  18.  * Revision 3.6  91/08/13  06:51:34  brennan
  19.  * VERSION .9994
  20.  * 
  21.  * Revision 3.5  91/08/03  05:20:11  brennan
  22.  * allow -ffile on cmdline
  23.  * 
  24.  * Revision 3.4  91/07/19  07:51:09  brennan
  25.  * escape sequence now recognized in command line assignments
  26.  * 
  27.  * Revision 3.3  91/06/28  04:16:49  brennan
  28.  * VERSION 0.999
  29.  * 
  30.  * Revision 3.2  91/06/19  10:23:41  brennan
  31.  * changes for xenix_r2, call this version 0.997
  32.  * 
  33.  * Revision 3.1  91/06/07  10:27:38  brennan
  34.  * VERSION 0.995
  35.  * 
  36.  * Revision 2.7  91/06/06  09:42:54  brennan
  37.  * proto for print_version
  38.  * 
  39.  * Revision 2.6  91/05/29  14:25:42  brennan
  40.  * -V option for version
  41.  * 
  42.  * Revision 2.5  91/05/28  09:04:52  brennan
  43.  * removed main_buff
  44.  * 
  45.  * Revision 2.4  91/05/22  07:46:50  brennan
  46.  * dependency cleanup for DOS
  47.  * 
  48.  * Revision 2.4  91/05/16  12:19:51  brennan
  49.  * cleanup of machine dependencies
  50.  * 
  51.  * Revision 2.3  91/05/15  12:07:39  brennan
  52.  * dval hash table for arrays
  53.  * 
  54.  * Revision 2.2  91/04/09  12:39:08  brennan
  55.  * added static to funct decls to satisfy STARDENT compiler
  56.  * 
  57.  * Revision 2.1  91/04/08  08:23:15  brennan
  58.  * VERSION 0.97
  59.  * 
  60. */
  61.  
  62.  
  63. /* init.c */
  64. #include "mawk.h"
  65. #include "code.h"
  66. #include "init.h"
  67. #include "memory.h"
  68. #include "symtype.h"
  69. #include "bi_vars.h"
  70. #include "field.h"
  71.  
  72. #ifdef THINK_C
  73. #include <console.h>
  74. #endif
  75.  
  76. #define PROGRAM_FROM_CMDLINE   1
  77.  
  78. /* static protos */
  79. static void PROTO( no_program, (void) ) ;
  80. static void PROTO( process_cmdline , (int, char **) ) ;
  81. static void PROTO( set_FS, (char *) ) ;
  82. static void PROTO( set_dump, (char *) ) ;
  83.  
  84. extern  void PROTO( print_version, (void) ) ;
  85.  
  86. #if  MSDOS  &&  ! HAVE_REARGV
  87. #include <fcntl.h>
  88. static  void  PROTO(emit_prompt, (void) ) ;
  89. #endif
  90.  
  91.  
  92. void initialize(argc, argv)
  93.   int argc ; char **argv ;
  94. {
  95.   bi_vars_init() ; /* load the builtin variables */
  96.   bi_funct_init() ; /* load the builtin functions */
  97.   kw_init() ; /* load the keywords */
  98.   field_init() ; 
  99.  
  100. #ifdef THINK_C
  101.   fputc('\n',stderr);    /* (causes Think C console window to appear) */
  102.   SetWTitle( FrontWindow(), "\pMacMAWK" );
  103.   argc = ccommand(&argv);
  104. #endif 
  105.   process_cmdline(argc, argv)  ;   
  106.  
  107.   jmp_stacks_init() ;
  108.   code_init() ;
  109.   fpe_init() ;
  110.  
  111. #if  ! HAVE_STRTOD
  112.   strtod_init() ;
  113. #endif
  114.  
  115. }
  116.  
  117. void  compile_cleanup()
  118. /* program has parsed OK, free some memory
  119.    we don't need anymore */
  120. {
  121.    scan_cleanup() ;
  122.    jmp_stacks_cleanup() ;
  123.    code_cleanup() ;
  124. }
  125.  
  126.  
  127. static void no_program()
  128. { errmsg( 0, "no program") ; mawk_exit(1) ; }
  129.  
  130. int  dump_code ;  /* if on dump internal code */
  131. #ifdef   DEBUG
  132. int  dump_RE   ;  /* if on dump compiled REs  */
  133. #endif
  134.  
  135. static void set_FS(s)
  136.   char *s ;
  137. {
  138.   rm_escape(s) ; /* recognize escape sequences */
  139.   cell_destroy(field+FS) ;
  140.   field[FS].type = C_STRING ;
  141.   field[FS].ptr = (PTR) new_STRING(s) ;
  142.   cast_for_split( cellcpy(&fs_shadow, field+FS) ) ;
  143. }
  144.  
  145. #ifdef  DEBUG
  146. static void set_dump(s)
  147.   char *s ;
  148. {
  149.   while ( 1 )
  150.   { switch ( *s )
  151.     { case 'p' :
  152.       case 'P' :  yydebug = 1 ; break ;
  153.  
  154.       case  'c' :
  155.       case  'C' :  dump_code = 1 ; break ;
  156.  
  157.       case  'r' :
  158.       case  'R' :  dump_RE = 1 ; break ;
  159.  
  160.       case   0  :  
  161.              if ( s[-1] == 'D' ) dump_code = 1 ;
  162.              return ;
  163.  
  164.       default :  break ;
  165.     }
  166.     s++ ;
  167.   }
  168. }
  169. #else
  170. static void  set_dump(s)
  171.   char *s ;
  172. { dump_code = 1 ; }
  173. #endif
  174.  
  175. static void process_cmdline(argc, argv)
  176.   int argc ; char **argv ;
  177. { extern int program_fd ; 
  178.   int i ;  /* index to walk command line */
  179.   char *p ;
  180.   CELL *cp ;
  181.   SYMTAB *st_p ;
  182.   CELL argi ; /* sets up ARGV */
  183.  
  184.  
  185.   for( i = 1 ; i < argc && argv[i][0] == '-' ; i++ )
  186.   { 
  187.     p = & argv[i][1] ;
  188.     switch( *p )
  189.     {
  190.       case 'F' :
  191.       set_FS(p+1) ; break  ;
  192.  
  193.       case 'D' :
  194.       set_dump(p+1) ; break ;
  195.  
  196.       case 'V' :
  197.       print_version() ; break ; /* no return */
  198.  
  199.       case 'f' :
  200.      /* check if program is glued to -f */
  201.       if ( p[1] == 0 ) /* normal -- not glued */
  202.       { if ( i == argc - 1 )  no_program() ;
  203.         scan_init(! PROGRAM_FROM_CMDLINE, argv[i+1] ) ;
  204.         i += 2 ;
  205.       }
  206.       else /* glued */
  207.       { scan_init(! PROGRAM_FROM_CMDLINE, p+1) ; i++ ; }
  208.  
  209.       goto  set_ARGV ;
  210.  
  211.       default : /* start of expression with uminus (hah) */
  212.       goto out ;
  213.     }
  214.   }
  215.  
  216. out:
  217.  
  218. #if  MSDOS   &&  ! HAVE_REARGV
  219. /* allows short programs to be typed in without mucking stdin */
  220.   emit_prompt() ;
  221.   scan_init(! PROGRAM_FROM_CMDLINE, "CON") ;
  222. #else   /* the real world */
  223.  
  224.   if ( i == argc )  no_program() ;
  225.   scan_init(PROGRAM_FROM_CMDLINE, argv[i]) ;
  226.   i++ ;
  227. #endif
  228.  
  229. set_ARGV:
  230.  
  231.   /* now set up ARGC and ARGV  */
  232.   st_p = insert( "ARGV" ) ;
  233.   st_p->type = ST_ARRAY ;
  234.   Argv = st_p->stval.array = new_ARRAY() ;
  235.   argi.type = C_DOUBLE ;
  236.   argi.dval = 0.0 ;
  237.   cp = array_find( st_p->stval.array, &argi, CREATE) ;
  238.   cp->type = C_STRING ;
  239.   cp->ptr = (PTR) new_STRING( progname ) ;
  240.  
  241.   /* ARGV[0] is set, do the rest 
  242.      The type of ARGV[1] ... should be C_MBSTRN
  243.      because the user might enter numbers from the command line */
  244.  
  245.     for( argi.dval = 1.0 ; i < argc ; i++, argi.dval += 1.0 )
  246.     { 
  247.       cp = array_find( st_p->stval.array, &argi, CREATE) ;
  248.       cp->type = C_MBSTRN ;
  249.       cp->ptr = (PTR) new_STRING( argv[i] ) ;
  250.     }
  251.     bi_vars[ARGC].type = C_DOUBLE ;
  252.     bi_vars[ARGC].dval = argi.dval ;
  253.  
  254. }
  255.  
  256. #if  MSDOS  &&  ! HAVE_REARGV
  257.  
  258. static void  emit_prompt()
  259. {  static char prompt[] = "mawk> " ;
  260.    int fd = open("CON", O_WRONLY, 0) ;
  261.  
  262.    (void) write(fd, prompt, strlen(prompt)) ;
  263.    (void) close(fd) ;
  264. }
  265. #endif
  266.  
  267.